/* 
store对象
*/
import {createStore, applyMiddleware, combineReducers, AnyAction} from 'redux'
import thunk, { ThunkDispatch } from 'redux-thunk'
import {composeWithDevTools} from 'redux-devtools-extension'
import counterReducer from './counter'
import userSearchReducer from './userSearch'

/* 
当应用中有多个模块的不同数据需要管理时  => 有多个reducer
解决: 使用redux提供combineReducers来合并多个reducer来管理所有模块的状态数据
    返回一个管理所有state的根/总reducer
    function (state, action) {} // state就是总的state: 包含所有模块的state
总的state的结构
    { 
      counter:  {
          count: 3
        },

      userSearch: {
          firstView: true,
          loading: false,
          errorMsg: '',
          users: []
        }
    }
*/
const rootReducer = combineReducers({
  counter: counterReducer,
  userSearch: userSearchReducer
})

// store.dispatch({type: 'xxx'})



// 创建store, 指定reducer函数, 应用上异步插件
const store = createStore(
  rootReducer, 
  composeWithDevTools(applyMiddleware<ThunkDispatch<any, any, AnyAction>>(thunk))
)

export default store

// 向外暴露根state的类型   store.getState函数返回值的类型
  // ReturnType: 用来得到函数返回值类型
  // typeof: 用来得到指定数据的类型
export type RootState = ReturnType<typeof store.getState>

// 向外暴露dispatch函数的类型
export type AppDispatch = typeof store.dispatch